<template>
  <div>
    <my-breadcrumb :items="['系统管理', '提醒设置']"/>
    <search :formItems="searchItems"/>
    <div class="add">
      <el-button type="primary" @click="toAdd">新增提醒</el-button>
    </div>
    <el-table :data="reminds">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="创建时间"
        prop="createtime">
      </el-table-column>
      <el-table-column
        label="间隔（小时）"
        prop="interval">
      </el-table-column>
      <el-table-column
        label="创建人"
        prop="createby">
      </el-table-column>
      <el-table-column
        label="提醒方式"
        prop="type">
      </el-table-column>
      <el-table-column
        label="闹钟文本"
        :show-overflow-tooltip="true"
        prop="content">
      </el-table-column>
      <el-table-column
        label="类型"
        prop="tixtype">
        <template slot-scope="scope">
          {{ scope.row.tixtype === '1' ? '开始前' : '开始后' }}
        </template>
      </el-table-column>
      <el-table-column
        label="时间选项"
        prop="stendtype">
        <template slot-scope="scope">
          {{ scope.row.stendtype === '1' ? '上架' : '下架' }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="140px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="toDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="isAdd ? '新增提醒' : '信息修改'" :visible.sync="showAdd">
      <el-form :model="remind" inline label-width="70px" ref="addForm">
        <el-form-item label="间隔（小时）" prop="interval" label-width="100px">
          <el-input v-model="remind.interval"/>
        </el-form-item>
        <el-form-item label="提醒方式" prop="type">
          <el-select v-model="remind.type">
            <el-option value="1" label="短信"/>
            <el-option value="2" label="推送"/>
            <el-option value="3" label="全部"/>
          </el-select>
        </el-form-item>
        <el-form-item label="闹钟文本" prop="content">
          <el-input v-model="remind.content"/>
        </el-form-item>
        <el-form-item label="类型" prop="tixtype">
          <el-select v-model="remind.tixtype">
            <el-option value="1" label="开始前"/>
            <el-option value="2" label="开始后"/>
          </el-select>
        </el-form-item>
        <el-form-item label="时间选项" prop="stendtype">
          <el-select v-model="remind.stendtype">
            <el-option value="1" label="上架"/>
            <el-option value="2" label="下架"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="footer" slot="footer">
        <el-button type="primary" @click="toSubmit">提交</el-button>
        <el-button type="danger" @click="showAdd = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {deleteRow} from '@/utils/getList'
export default {
  name: 'remind',
  data () {
    return {
      searchItems: [
      ], // 检索结构
      reminds: [],
      remind: {
        interval: '',
        type: '',
        content: '',
        tixtype: '',
        stendtype: ''
      },
      showAdd: false,
      isAdd: true,
      updateRow: null
    }
  },
  methods: {
    getList () {
      this.axios.get('/Config/getremindtype')
        .then(res => {
          if (res.data.status === 1) {
            this.reminds = res.data.data.list
          }
        })
    },

    toAdd () {
      this.isAdd = true
      this.showAdd = true
      this.$nextTick(() => {
        this.$refs['addForm'].resetFields()
      })
    },
    toUpdate (row) {
      this.isAdd = false
      this.showAdd = true
      this.updateRow = row
      this.$nextTick(() => {
        Object.keys(this.remind).forEach(key => {
          this.remind[key] = row[key]
        })
      })
    },
    toDelete (id) {
      deleteRow({url: '/Config/delete', id}, (res) => {
        if (res.data.status === 1) {
          this.$message.success(res.data.msg)
          this.getList()
        }
      })
    },
    toSubmit () {
      let url = '/Config/addremindtype'
      let params = {}
      Object.keys(this.remind).forEach(key => {
        params[key] = this.remind[key]
      })
      if (!this.isAdd) {
        url = '/Config/editremindtype'
        params.id = this.updateRow.id
      }
      this.axios.post(url, this.qs.stringify(params))
        .then(res => {
          if (res.data.status === 1) {
            this.showAdd = false
            this.$message.success(res.data.msg)
            this.getList()
          }
        })
    }
  },
  created () {
    this.getList()
  }
}
</script>

<style scoped>

</style>
